
    var ul;
    var li;
    var cities;
    var div;
    var city;
    var xmlhttp=new XMLHttpRequest();
    var selected_city;
    var selected_li = -1;

function createtags()
{

    ul = document.createElement('ul');
    ul.setAttribute('id', 'list');
    document.tripform.country.disabled = true;
    city =  document.getElementById('city');
    div = document.getElementById('livesearch');
    div.style.left = city.offsetLeft +'px';
    div.style.top = city.offsetTop + city.offsetHeight +'px';
    div.style.width = city.offsetWidth +'px';
    div.style.visibility = 'hidden';
    //alert(city.offsetLeft+' '+div.offsetLeft);
    document.tripform.country.value = '';

}

function showResult(str)
{
  xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState==4){        
        if(str==xmlhttp.responseText)
                           {
                               handleSelect(str);
                           }

        document.tripform.country.value = '';
        if (str.length >= 2 && xmlhttp.responseText !=cities){
            selected_li = -1;
               var mySplitResult = xmlhttp.responseText.split("/");

                   var limit = ul.children.length;
                        for(k = 0; k < limit; k++){
                            ul.removeChild(ul.children[0]);
                        };
              
                   for(i = 0; i < mySplitResult.length; i++){
                       
                       li = document.createElement('li');
                       li.setAttribute('onclick', 'handleSelect(this.innerHTML)');
                       li.innerHTML = mySplitResult[i];
                       ul.appendChild(li);
                   };
               div.appendChild(ul);
               cities = xmlhttp.responseText;
         };
         if(xmlhttp.responseText.length == 0){
               div.style.visibility = 'hidden';
         }
         else
               div.style.visibility = 'visible';

         
    };

    if (str.length < 2)
             {
                 document.tripform.country.disabled = true;
                 div.style.visibility = 'hidden';
             }
    if (xmlhttp.responseText.length == 0 && str.length >= 2)
             {
                 document.tripform.country.disabled = false;
             }
     if (xmlhttp.responseText.length != 0)
             {
                 document.tripform.country.disabled = true;
             }
                   
 }

        xmlhttp.open("GET","liveSearch.php?query="+str+"&getCountry=0",true);
        xmlhttp.send();

   
}

function showCountry(selected_city)
{
      
      xmlhttp.onreadystatechange=function()
      {
        if (xmlhttp.readyState==4){
            //alert(xmlhttp.responseText);
            document.tripform.country.value = xmlhttp.responseText;
            document.tripform.country.disabled = true;
        }
      }

      xmlhttp.open("GET","liveSearch.php?query="+selected_city+"&getCountry=1",true);
      xmlhttp.send();


}


function searchCountry(possible_city)
{
      xmlhttp.onreadystatechange=function()
      {
        if (xmlhttp.readyState==4){
            //alert(xmlhttp.responseText);
            if(xmlhttp.responseText.length == 0)
            document.tripform.country.disabled = false;
            else{
                document.tripform.country.disabled = true;
                document.tripform.country.value = xmlhttp.responseText;
            }

        }
      }

      xmlhttp.open("GET","liveSearch.php?query="+possible_city+"&getCountry=1",true);
      xmlhttp.send();
}


function handleSelect(text)
{
    document.tripform.city.value = text.toString();
    showCountry(text.toString());
    div.style.visibility = 'hidden';
    document.tripform.country.disabled = true;
}

function handleArrows(e)
{
        
        switch(e.keyCode)
        {
        case 40:
            if(selected_li == -1 || selected_li == document.getElementById('list').children.length-1){
                handleLis(selected_li,0);
            }
            else{
                handleLis(selected_li,++selected_li);
            }
            break;
        case 38:
            if(selected_li == 0){
                handleLis(selected_li,document.getElementById('list').children.length-1);
            }
            else
                handleLis(selected_li,--selected_li);
            break;
        case 39:
            handleSelect(document.getElementById('list').children[selected_li].innerHTML);
            handleLis(selected_li,-1);
            break;
        case 13:
            handleSelect(document.getElementById('list').children[selected_li].innerHTML);
            handleLis(selected_li,-1);
            break;
        }


}

function handleLis(previous,current){
    if(previous != -1){
        document.getElementById('list').children[previous].style.color = "black";
        document.getElementById('list').children[previous].style.backgroundColor = "white";
    }
    if(current != -1){
        document.getElementById('list').children[current].style.color = "white";
        document.getElementById('list').children[current].style.backgroundColor = "grey";
    }
    selected_li = current;
}

function disableEnter(e){
     return (e.keyCode != 13);
}

function hideDiv()
{
     div.style.visibility = 'hidden';
}

